package com.ithei.date.mydate;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatter格式化 {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.of(2021, 12, 21, 14, 22, 36);
        String format = ldt.format(dtf);
        System.out.println("格式化LocalDateTime日期:  " + format);
        System.out.println(format);
        LocalDateTime parse = LocalDateTime.parse(format, dtf);
        System.out.println("解析日期字符串:  " + parse);

        LocalDate ld = ldt.toLocalDate();
        System.out.println("LocalDateTime对象转换成LocalDate对象格式: " + ld);
        LocalTime lt = ldt.toLocalTime();
        System.out.println("LocalDateTime对象转换成LocalTime对象格式" + lt);
        int monthValue = ld.getMonthValue();
        int year = ld.getYear();
        int dayOfYear = ld.getDayOfYear();
        int dayOfMonth = ld.getDayOfMonth();
        DayOfWeek dayOfWeek = ld.getDayOfWeek();
        int value = dayOfWeek.getValue();
        int hour = lt.getHour();
        int minute = lt.getMinute();
        int second = lt.getSecond();
        //增加年份或者减少年份
        LocalDateTime localDateTime1 = ldt.plusYears(-1);
        System.out.println(localDateTime1.format(dtf));
        LocalDateTime localDateTime = ldt.plusYears( 1);
        System.out.println(localDateTime.format(dtf));
        LocalDateTime localDateTime2 = ldt.withDayOfMonth(22);
        LocalDateTime localDateTime3 = ldt.withDayOfYear(222);
        LocalDateTime localDateTime4 = ldt.withHour(22);
        LocalDateTime localDateTime5 = ldt.withMinute(33);
        LocalDateTime localDateTime6 = ldt.withSecond(44);
        LocalDateTime localDateTime7 = ldt.withYear(2011);
        LocalDateTime localDateTime8 = ldt.withMonth(7);
        LocalDateTime ldt1=LocalDateTime.now();
        LocalDate lt1=ldt.toLocalDate();
        LocalDate lt2=ldt1.toLocalDate();
        Period between = Period.between(lt1, lt2);
        System.out.println(between);
        System.out.println(between.getYears());
        LocalDateTime localDateTime12=LocalDateTime.of(2020,6,15,14,13,12);
        LocalDateTime localDateTime13=LocalDateTime.now().withHour(14).withMinute(13).withSecond(12);
        Duration dt=Duration.between(localDateTime12,localDateTime13);
        long l = dt.toDays();
        System.out.println("差的天数:   "+l);
        long l1 = dt.toHours();
        System.out.println("差的小时:   "+l1);
        long l2 = dt.toMinutes();
        System.out.println("差的分钟数:  "+l2);

    }
}
